﻿using System;
using HISModels.ViewModel;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using KeanuMvc.Reference;
using HIS.Utils;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using HISModels.Entities;
using System.Configuration;
using HIS.Utils.Securities;
using HISModels.ViewModel;
using System.Text;

namespace HIS.Controllers.Master
{
    public class Doctors : HIS.Controllers.AppBaseController 
    {
        #region Fields
        private string strAction = string.Empty;
        private string strNotification = string.Empty;
        private HISModels.Entities.Doctor doctor = null;
        private HISModels.Entities.DoctorHuRel doctorHuRel = null;
        private HISModels.Entities.DoctorSpecRel doctorSpecRel = null;
        private HISModels.Entities.Employee emp = null;
        #endregion

        #region Doctor
        public Doctors()
        {
            Data.Add("Pagetitle", " Physician");
            Data.Add("Notification", strNotification);
        }

        [Authorization(RoleAction = "master-doctor")]
        public override void Index()
        {
           
            try
            {
                Data["Pagetitle"] += " :: List Doctors ";

                string ddlHospitals = DropDownHospital("");
                Data.Add("ddlHospitals", ddlHospitals);

                string ddlGroupSpecialist = DropDownGroupSpecialist("");
                Data.Add("ddlGroupSpecialist", ddlGroupSpecialist);

                string ddlSpecialist = DropDownSpecialist("");
                Data.Add("ddlSpecialists", ddlSpecialist);

                string ddlSubSpecialist = DropDownSubSpecialist("");
                Data.Add("ddlSubSpecialist", ddlSubSpecialist);

                RenderFile("master\\doctor.html");

            }
            catch (Exception ex)
            {
                HttpContext.Current.Response.Write(ex.Message);
            }
        }

        [Authorization(RoleAction = "master-doctor")]
        public string DropDownHospital(string Selected)
        {
            StringReader stringReaderDropDown = null;
            string strResult = string.Empty;
            string Name = string.Empty;
            string strUri = "";
            strUri =Global.ServiceUriDictionary["hospital"] +Global.ServiceUriDictionary["requestformat"];
            try
            {
                stringReaderDropDown = base.RestService.GetJson(strUri);
                List<HISModels.Entities.RefHu> listSp = JsonConvert.DeserializeObject<List<RefHu>>(stringReaderDropDown.ReadToEnd());
                List<object[]> activeArr = new List<object[]>();

                activeArr.Add(new object[] { "", Name = "- All Hospitals -" });


                foreach (RefHu item in listSp) activeArr.Add(new object[] { item.Id, Name = item.NameValue });

                strResult = KeanuMvc.Reference.Html.DropDown("IdHospital", string.Empty, activeArr, Selected);
                activeArr.Clear();
                activeArr = null;
            }
            catch
            {
            }
            finally
            {
                
            }
            return strResult;
        }

        [Authorization(RoleAction = "master-doctor")]
        public string DropDownGroupSpecialist(string Selected)
        {
            StringReader stringReaderDropDown = null;
            string strResult = string.Empty;
            string Name = string.Empty;
            string strUri = "";

            strUri = Global.ServiceUriDictionary["groupspec"] + Global.ServiceUriDictionary["requestformat"];
            try
            {
                stringReaderDropDown = base.RestService.GetJson(strUri);
                List<RefSpecialistGroup> listSp = JsonConvert.DeserializeObject<List<RefSpecialistGroup>>(stringReaderDropDown.ReadToEnd());
                List<object[]> activeArr = new List<object[]>();

                activeArr.Add(new object[] { "", Name = "- All Group Specialist -" });

                foreach (RefSpecialistGroup item in listSp) activeArr.Add(new object[] { item.Id, Name = item.NameValue });

                strResult = KeanuMvc.Reference.Html.DropDown("IdGroupSpecialist", string.Empty, activeArr, Selected);
                activeArr.Clear();
                activeArr = null;
            }
            catch
            {
            }
            finally
            {
                stringReaderDropDown.Dispose();
            }
            return strResult;
        }


        [Authorization(RoleAction = "master-doctor")]
        public string DropDownSpecialist(string Selected)
        {
            StringReader stringReaderDropDown = null;
            string strResult = string.Empty;
            string Name = string.Empty;
            string strUri = "";

            strUri =Global.ServiceUriDictionary["doctorspecialist"] +Global.ServiceUriDictionary["requestformat"];
            try
            {
                stringReaderDropDown = base.RestService.GetJson(strUri);
                List<RefSpecialist> listSp = JsonConvert.DeserializeObject<List<RefSpecialist>>(stringReaderDropDown.ReadToEnd());
                List<object[]> activeArr = new List<object[]>();

                activeArr.Add(new object[] {"", Name = "- All Specialist -" });


                foreach (RefSpecialist item in listSp) activeArr.Add(new object[] { item.Id, Name = item.NameValue });

                strResult = KeanuMvc.Reference.Html.DropDown("IdSpecialist", string.Empty, activeArr, Selected);
                activeArr.Clear();
                activeArr = null;
            }
            catch
            {
            }
            finally
            {
                stringReaderDropDown.Dispose();
            }
            return strResult;
        }

        [Authorization(RoleAction = "master-doctor")]
        public string DropDownSubSpecialist(string Selected)
        {
            StringReader stringReaderDropDown = null;
            string strResult = string.Empty;
            string Name = string.Empty;
            string strUri = "";

            strUri = Global.ServiceUriDictionary["subspec"] + Global.ServiceUriDictionary["requestformat"];
            try
            {
                stringReaderDropDown = base.RestService.GetJson(strUri);
                List<RefSubSpecialist> listSp = JsonConvert.DeserializeObject<List<RefSubSpecialist>>(stringReaderDropDown.ReadToEnd());
                List<object[]> activeArr = new List<object[]>();

                activeArr.Add(new object[] { "", Name = "- All Sub Specialist -" });


                foreach (RefSubSpecialist item in listSp) activeArr.Add(new object[] { item.Id, Name = item.NameValue });

                strResult = KeanuMvc.Reference.Html.DropDown("SubSpecialistId", string.Empty, activeArr, Selected);
                activeArr.Clear();
                activeArr = null;
            }
            catch
            {
            }
            finally
            {
                stringReaderDropDown.Dispose();
            }
            return strResult;
        }

        [Authorization(RoleAction = "master-doctor")]
        public string DropDownItemGroupSpecialist(string selected)
        {
            StringReader stringReaderDropDown = null;
            string strResult = string.Empty;
            string strUri = Global.ServiceUriDictionary["groupspec"] + Global.ServiceUriDictionary["requestformat"];
            try
            {
                stringReaderDropDown = base.RestService.GetJson(strUri);
                List<RefSpecialistGroup> listRole = JsonConvert.DeserializeObject<List<RefSpecialistGroup>>(stringReaderDropDown.ReadToEnd());
                List<object[]> activeArr = new List<object[]>();
                activeArr.Add(new object[] { "-1", "--Select--" });
                foreach (RefSpecialistGroup item in listRole) activeArr.Add(new object[] { item.Id, item.NameValue });
                strResult = KeanuMvc.Reference.Html.DropDown("IdGroupSpecialist", string.Empty, activeArr, selected);
                activeArr.Clear();
                activeArr = null;
            }
            catch
            {
            }
            finally
            {
                stringReaderDropDown.Dispose();
            }
            return strResult;
        }

        [Authorization(RoleAction = "master-doctor")]
        public string DropDownItemSpecialist(string selected)
        {
            
        StringBuilder returnValue = new StringBuilder();
            StringReader stringReaderDropDown = null;
            string groupspecId = "";
            string strResult = string.Empty;
            string strUri = Global.ServiceUriDictionary["doctorspecialist"] + Global.ServiceUriDictionary["requestformat"];
            try {
                stringReaderDropDown = base.RestService.GetJson(strUri);
                List<RefSpecialist> listRole = JsonConvert.DeserializeObject<List<RefSpecialist>>(stringReaderDropDown.ReadToEnd());
                List<object[]> activeArr = new List<object[]>();
                returnValue.Append(" <select name='SpecialistId' id='SpecialistId'>  ");
                returnValue.Append("<option idParent='0' value='0'> -- Select --</option>");
                foreach (RefSpecialist item in listRole)
                {
                    if (item.Id.ToString().Equals(selected)) {
                        groupspecId = Convert.ToString(item.GroupSpecialistId);
                        Data.Add("ddlItemGroupSpecialist", DropDownItemGroupSpecialist(groupspecId.NullSafeToString()));
                        returnValue.Append("<option idParent='" + item.GroupSpecialistId + "' value='" + item.Id + "' selected='selected'>" + item.NameValue + "</option>");
                    } else {

                        returnValue.Append("<option idParent='" + item.GroupSpecialistId + "' value='" + item.Id + "'>" + item.NameValue + "</option>");
                    }
                }  
                returnValue.Append("</select>");
                if (selected == "") {
                    Data.Add("ddlItemGroupSpecialist", DropDownItemGroupSpecialist(groupspecId.NullSafeToString()));
                }
                activeArr.Clear();
                activeArr = null;
            } catch {
            } finally {
                stringReaderDropDown.Dispose();
            }
            return returnValue.ToString();
        }

        [Authorization(RoleAction = "master-doctor")]
        public string DropDownItemSubSpecialist(string selected)
        {

            StringBuilder returnValue = new StringBuilder();
            StringReader stringReaderDropDown = null;
            string specId = "";
            string strResult = string.Empty;
            string strUri = Global.ServiceUriDictionary["subspec"] + Global.ServiceUriDictionary["requestformat"];
            try
            {
                stringReaderDropDown = base.RestService.GetJson(strUri);
                List<RefSubSpecialist> listRole = JsonConvert.DeserializeObject<List<RefSubSpecialist>>(stringReaderDropDown.ReadToEnd());
                List<object[]> activeArr = new List<object[]>();
                returnValue.Append(" <select name='SubSpecialistId' id='SubSpecialistId'>  ");
                returnValue.Append("<option idParent='0' value='0'> -- Select --</option>");
                foreach (RefSubSpecialist item in listRole)
                {
                    if (item.Id.ToString().Equals(selected))
                    {
                        specId = Convert.ToString(item.SpecialistId);
                        Data.Add("ddlItemSpecialist", DropDownItemSpecialist(specId.NullSafeToString()));
                        returnValue.Append("<option idParent='" + item.SpecialistId + "' value='" + item.Id + "' selected='selected'>" + item.NameValue + "</option>");
                    }
                    else
                    {

                        returnValue.Append("<option idParent='" + item.SpecialistId + "' value='" + item.Id + "'>" + item.NameValue + "</option>");
                    }
                }
                returnValue.Append("</select>");
                if (selected == "")
                {
                    Data.Add("ddlItemSpecialist", DropDownItemGroupSpecialist(specId.NullSafeToString()));
                }
                activeArr.Clear();
                activeArr = null;
            }
            catch
            {
            }
            finally
            {
                stringReaderDropDown.Dispose();
            }
            return returnValue.ToString();
        }

        [Authorization(RoleAction = "master-doctor")]
        public void TableList()
        {
            
            try
            {
                string strJson = string.Empty;
                string sEcho = HttpContext.Current.Request["sEcho"];

                string hospitalid = HttpContext.Current.Request["IdHospital"].NullSafeToString();
                string groupspecialistid = HttpContext.Current.Request["IdGroupSpecialist"].NullSafeToString();
                string specialist = HttpContext.Current.Request["IdSpecialist"].NullSafeToString();
                string subspecialist = HttpContext.Current.Request["IdSubSpecialist"].NullSafeToString();

                string ssearch = "hospitalid=" + hospitalid + ";groupspecialistid=" + groupspecialistid + ";specialist=" + specialist + ";subspecialist=" + subspecialist;

                PostDataTablesNet pagingSearch = new PostDataTablesNet();
                pagingSearch.iDisplayLength = Convert.ToInt32(HttpContext.Current.Request["iDisplayLength"]);
                pagingSearch.iDisplayStart = Convert.ToInt32(HttpContext.Current.Request["iDisplayStart"]);
                if (!string.IsNullOrEmpty(ssearch))
                {
                    pagingSearch.sSearch = ssearch;
                }
                else { pagingSearch.sSearch = Convert.ToString(HttpContext.Current.Request["sSearch"]); }

                pagingSearch.sEcho = Convert.ToInt32(HttpContext.Current.Request["sEcho"]);
                pagingSearch.sSearch = ssearch;

                string strUri = "";
                strUri =Global.ServiceUriDictionary["doctorpaging"] +Global.ServiceUriDictionary["requestformat"];
                StringReader stringReader = base.RestService.PostJson(strUri, JsonConvert.SerializeObject(pagingSearch));


                strJson = stringReader.ReadToEnd();
                stringReader.Dispose();
                base.RestService.Dispose();


                HttpContext.Current.Response.Write(strJson);

            }
            catch (Exception ex)
            {
                HttpContext.Current.Response.Write(ex.Message);
            }
        }

        [Authorization(RoleAction = "master-doctor")]
        public void EditDoctor()
        {
            StringReader stringReader = null;
            int id = 0;
            try
            {
                id = Convert.ToInt32(HttpContext.Current.Request["id"]);
            }
            catch
            {
                GoToUrl("doctors/"); return;
            }

            try
            {
                ///Set Rest Url///
                HISModels.Entities.Doctor doctor = new HISModels.Entities.Doctor();
                HISModels.Entities.Employee emp = new HISModels.Entities.Employee();
                string strUri =Global.ServiceUriDictionary["doctor"] + id +Global.ServiceUriDictionary["requestformat"];
                ///Get Rest///
                stringReader = base.RestService.GetJson(strUri);

                ///Deserialize json response to object///
                ViewDataDoctor vdoctor = JsonConvert.DeserializeObject<ViewDataDoctor>(stringReader.ReadToEnd());
                
               
                doctor.Id = vdoctor.DoctorId;
                doctor.CodeValue =vdoctor.DoctorCode;
                doctor.EmployeeId = vdoctor.EmployeeId;
                Data.Add("doctor", doctor);
                int idSubSpec = vdoctor.SubSpecialistId;
                int idGroupSpec = vdoctor.GroupSpecialistId;
                int idSpec = vdoctor.SpecialistId;

                emp.FirstName = vdoctor.FirstName;
                emp.LastName = vdoctor.LastName;
                Data.Add("emp", emp);
                
                Data.Add("id", id);
                Data.Add("IsCreate", "false");
                Data.Add("ddlEditGroupSpecialist", DropDownItemGroupSpecialist(Convert.ToString(idGroupSpec).NullSafeToString()));
                Data.Add("ddlEditSpecialist", DropDownItemSpecialist(Convert.ToString(idSpec).NullSafeToString()));
                Data.Add("ddlEditSubSpecialist", DropDownItemSubSpecialist(Convert.ToString(idSubSpec).NullSafeToString()));
                Data["Notification"] = strNotification;
            }
            catch (Exception ex)
            {
                Data["Notification"] = Helper.ErrorMessage(ex.Message);
            }
            finally
            {

                RenderFile("Master\\editdoctor.html");
                stringReader.Dispose();
                base.RestService.Dispose();
            }
        }


        [Authorization(RoleAction = "master-doctor")]
        public void DeleteDoctor()
        {
            int id = 0;
            try
            {
                id = Convert.ToInt32(HttpContext.Current.Request["id"]);
            }
            catch
            {
                return;
            }
            try
            {
                string strUri = Global.ServiceUriDictionary["doctor"] + Global.ServiceUriDictionary["requestformat"];
                StringReader strReader = base.RestService.DeleteJson(strUri, JsonConvert.SerializeObject(id));
                doctor = JsonConvert.DeserializeObject<Doctor>(strReader.ReadToEnd());

                if (doctor != null)
                {
                    strNotification = Helper.SuccessMessage(base.RestService.ObjectResponse.Headers[HISModels.Enums.ResponseMessage.DetailHeadesMessageKey].ToString());
                }
                else
                {
                    strNotification = Helper.ErrorMessage(base.RestService.ObjectResponse.Headers[HISModels.Enums.ResponseMessage.DetailHeadesMessageKey].ToString());
                }
                strNotification = "{\"strNotification\": \"" + strNotification + "\"}";

            }
            catch (Exception ex)
            {
                Data["Notification"] = Helper.ErrorMessage(ex.Message);
            }
            finally
            {
                HttpContext.Current.Response.Write(strNotification);
                base.RestService.Dispose();
            }
        }


        [Authorization(RoleAction = "master-doctor")]
        public void CreateDoctor()
        {
            string ddlEditGroupSpecialist = DropDownItemGroupSpecialist("");
            Data.Add("ddlEditGroupSpecialist", ddlEditGroupSpecialist);

            string ddlEditSpecialist = DropDownItemSpecialist("");
            Data.Add("ddlEditSpecialist", ddlEditSpecialist);

            string ddlEditSubSpecialist = DropDownItemSubSpecialist("");
            Data.Add("ddlEditSubSpecialist", ddlEditSubSpecialist);

            try
            {
                Data["Pagetitle"] += " :: Add Doctor";
                doctor = new HISModels.Entities.Doctor() { CodeValue = string.Empty};
                emp=new HISModels.Entities.Employee(){ FirstName= string.Empty, LastName=string.Empty };
                doctorHuRel = new HISModels.Entities.DoctorHuRel() { DoctorId= 0, HospitalUnitId=0 };
                doctorSpecRel = new HISModels.Entities.DoctorSpecRel() { DoctorId = 0, SubSpecialistId = 0 };
                Data.Add("doctor", doctor);
                
                Data.Add("IsCreate", "true");
                //Data.Add("ddlEditGroupSpecialist", DropDownItemGroupSpecialist(idGroupSpec);
                //Data.Add("ddlEditSpecialist", DropDownItemSpecialist(Convert.ToString(idSpec).NullSafeToString()));
                //Data.Add("ddlEditSubSpecialist", DropDownItemSubSpecialist(Convert.ToString(idSubSpec).NullSafeToString()));
                Data["Notification"] = strNotification;

            }
            catch (Exception ex)
            {
                Data["Notification"] = Helper.ErrorMessage(ex.Message);
            }
            finally
            {

                RenderFile("Master\\generatedoctorcode.html");
                base.RestService.Dispose();
            }
        }

        [Authorization(RoleAction = "master-doctor")]
        public void SearchEmployee()
        {
            try
            {
                //Data.Add("ddlocation", DropDownHospital(string.Empty));
                string id = HttpContext.Current.Request["id"];
                Data.Add("HusId", id);
                RenderFile("master\\searchemployee.html");
            }
            catch (Exception ex)
            {
                HttpContext.Current.Response.Write(ex.Message);
            }
        }

        [Authorization(RoleAction = "master-doctor")]
        public void SearchEmployeeTableList()
        {
            try
            {
                ///--------- Sample post to rest and get record as paging-------------/// 
                ///Create paging object///
                PostDataTablesNet pagingSearch = new PostDataTablesNet();
                pagingSearch.iDisplayLength = Convert.ToInt32(HttpContext.Current.Request["iDisplayLength"]);
                pagingSearch.iDisplayStart = Convert.ToInt32(HttpContext.Current.Request["iDisplayStart"]);

                int id = Convert.ToInt32(HttpContext.Current.Request["id"]);
                string employeenumber = HttpContext.Current.Request["employeenumber"].NullSafeToString();
                string name = HttpContext.Current.Request["name"].NullSafeToString();
                //int hospitalid = Convert.ToInt32(HttpContext.Current.Request["hospitalid"]);

                pagingSearch.sSearch = "employeenumber=" + employeenumber + ";name=" + name + ";id=" + id;

                pagingSearch.sEcho = Convert.ToInt32(HttpContext.Current.Request["sEcho"]);
                //
                ///Set Rest Url///
                string strUri = Global.ServiceUriDictionary["employeepaging"] + Global.ServiceUriDictionary["requestformat"];
                ///Post paging object to rest as json string///
                StringReader stringReader = base.RestService.PostJson(strUri, JsonConvert.SerializeObject(pagingSearch));
                ///Read response value///
                string strJson = stringReader.ReadToEnd();
                ///----------Consistenly clean up all object------------///
                stringReader.Dispose();
                base.RestService.Dispose();
                HttpContext.Current.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                HttpContext.Current.Response.Write(ex.Message);
            }
        }
       
        [Authorization(RoleAction = "master-doctor")]
        public void SaveDoctor()
        {

            StringReader stringReader = null;
            string strUri = string.Empty;
            try
            {
                ViewDataDoctor vdoctor = new ViewDataDoctor();
                doctor = new Doctor(); Utils.Helper.SetNullToEmptyString(doctor);
                doctorSpecRel = new DoctorSpecRel(); Utils.Helper.SetNullToEmptyString(doctorSpecRel);
                doctorHuRel = new DoctorHuRel(); Utils.Helper.SetNullToEmptyString(doctorHuRel);
                emp = new Employee(); Utils.Helper.SetNullToEmptyString(emp);
                if (HttpContext.Current.Request["IsCreate"] != "true")
                {
                    int id = 0;
                    try
                    { 
                        id = Convert.ToInt32(HttpContext.Current.Request["Id"]);
                    }
                    catch { }
                    strUri = Global.ServiceUriDictionary["doctor"] + id + Global.ServiceUriDictionary["requestformat"];
                    ///Get Rest///
                    stringReader = base.RestService.GetJson(strUri);
                    vdoctor = JsonConvert.DeserializeObject<ViewDataDoctor>(stringReader.ReadToEnd());
                    //emp = JsonConvert.DeserializeObject<Employee>(stringReader.ReadToEnd());
                }

                doctor.PostHandler<HISModels.Entities.Doctor>();
                doctor.CodeValue = vdoctor.DoctorCode;
                doctor.RecordStatus = vdoctor.RecordStatus;
                doctorSpecRel.PostHandler<HISModels.Entities.DoctorSpecRel>();
                doctorSpecRel.DoctorId = vdoctor.DoctorId;
                doctorSpecRel.Id = vdoctor.DoctorSpecRelId; 
                //doctorHuRel.PostHandler<HISModels.Entities.DoctorHuRel>();
                emp.PostHandler<HISModels.Entities.Employee>();
                emp.Id = HttpContext.Current.Request["EmpId"] != null ? Convert.ToInt32(HttpContext.Current.Request["EmpId"]) : vdoctor.EmployeeId;
                emp.EmployeeNumber = vdoctor.EmployeeNumber;
                emp.LastName = vdoctor.LastName;
                emp.FirstName = vdoctor.FirstName;

                doctor.DoctorSpecRels.Add(doctorSpecRel);
                doctor.EmployeeId = emp.Id;
                
                //doctor.DoctorHuRels.Add(doctorHuRel);
                emp.Doctors.Add(doctor);


                strUri = Global.ServiceUriDictionary["employee"] + Global.ServiceUriDictionary["requestformat"];

                if (HttpContext.Current.Request["IsCreate"] == "true")
                {
                    stringReader = base.RestService.PostJson(strUri, JsonConvert.SerializeObject(doctor));
                }
                else
                {
                    stringReader = base.RestService.PutJson(strUri, JsonConvert.SerializeObject(emp));
                }


                doctor = JsonConvert.DeserializeObject<Doctor>(stringReader.ReadToEnd());

                if (doctor != null)
                {
                    strNotification = Helper.SuccessMessage(base.RestService.ObjectResponse.Headers[HISModels.Enums.ResponseMessage.DetailHeadesMessageKey].ToString());
                }
                else
                {
                    strNotification = Helper.ErrorMessage(base.RestService.ObjectResponse.Headers[HISModels.Enums.ResponseMessage.DetailHeadesMessageKey].ToString());
                }


                strNotification = "{\"strNotification\": \"" + strNotification + "\"}";
                HttpContext.Current.Response.Write(strNotification);

            }
            catch (Exception ex)
            {
                strNotification = ex.Message;
            }
            finally
            {

                base.RestService.Dispose();
            }
        }

        [Authorization(RoleAction = "master-doctor")]
        public void SaveDoctorCode()
        {
            StringReader stringReader = null;
            string strUri = string.Empty;
            try
            {
                ViewDataDoctor vdoctor = new ViewDataDoctor();
                doctor = new Doctor(); Utils.Helper.SetNullToEmptyString(doctor);
                doctorSpecRel = new DoctorSpecRel(); Utils.Helper.SetNullToEmptyString(doctorSpecRel);
                if (HttpContext.Current.Request["IsCreate"] != "true")
                {
                    string id = "";
                    try
                    {
                        id = HttpContext.Current.Request["id"];
                    }
                    catch { }
                    strUri = Global.ServiceUriDictionary["doctor"] + id + Global.ServiceUriDictionary["requestformat"];
                    ///Get Rest///
                    stringReader = base.RestService.GetJson(strUri);
                    doctor = JsonConvert.DeserializeObject<Doctor>(stringReader.ReadToEnd());
                }

                
                vdoctor.PostHandler<HISModels.Entities.Doctor>();
                //doctorSpecRel.PostHandler<HISModels.Entities.DoctorSpecRel>();
                //vdoctor.PostHandler<HISModels.Entities.VDoctor>();
                int idGroupSpec = vdoctor.GroupSpecialistId;
                int idSpec = vdoctor.SpecialistId;
                int idSubSpec = vdoctor.SubSpecialistId;
                doctor.CodeValue = vdoctor.GroupSpecialistId.ToString() + vdoctor.SpecialistId.ToString() + vdoctor.SubSpecialistId.ToString();
                //int doctorCode= idGroupSpec 
                doctor.DoctorSpecRels.Add(doctorSpecRel);
                strUri = Global.ServiceUriDictionary["doctor"] + Global.ServiceUriDictionary["requestformat"];

                if (HttpContext.Current.Request["IsCreate"] == "true")
                {
                    stringReader = base.RestService.PostJson(strUri, JsonConvert.SerializeObject(doctor));
                }
                else
                {
                    stringReader = base.RestService.PutJson(strUri, JsonConvert.SerializeObject(doctor));
                }


                doctor = JsonConvert.DeserializeObject<Doctor>(stringReader.ReadToEnd());

                if (doctor != null)
                {
                    strNotification = Helper.SuccessMessage(base.RestService.ObjectResponse.Headers[HISModels.Enums.ResponseMessage.DetailHeadesMessageKey].ToString());
                }
                else
                {
                    strNotification = Helper.ErrorMessage(base.RestService.ObjectResponse.Headers[HISModels.Enums.ResponseMessage.DetailHeadesMessageKey].ToString());
                }


                strNotification = "{\"strNotification\": \"" + strNotification + "\"}";
                HttpContext.Current.Response.Write(strNotification);

            }
            catch (Exception ex)
            {
                strNotification = ex.Message;
            }
            finally
            {

                base.RestService.Dispose();
            }
        }

        #endregion
    }
}

        






